$(function(){
    var appInfo = new AppInfo();
    appInfo.init().init();
});


function AppInfo(){
    this.init = function(){
        return APPINFO;
    };
    var APPINFO = {
        init : function(){
            $.util.initAppTagLink($('span.s_app_list_tag'), SYSTEM_DATA.contextPath);
            APPINFO.listeners.bindEvents();
            
            APPINFO.funs.initScore();
        },
        listeners : {
            bindEvents : function(){
                $.util.foreachFunction(this, [ "bindEvents" ]);
            },
            bindDownLoad : function(){
                $("#a_app_info_download").click(function(){
                    var $this = $(this);
                    $.post(DATA_INFO.contextPath + '/Index/appDownLoad.shtml', {
                        'id' : $this.attr('app_id')
                    }, function(jsonResult){}, 'json');
                });
            },
            bindAppTag : function(){
                $('a.a_app_list_tag').click(function(){
                    $.util.initHiddenFormSubmit($('#frm_hidden_app_info'),SYSTEM_DATA.contextPath + '/Index/appTypeList.shtml',  [{
                        'name' : 'type', 
                        'value' : $(this).attr('tag_text')
                    }]);
                });
            },
            bindComment : function(){
                $("#a_app_info_comment").click(function(){
                    APPINFO.funs.ajaxIsLogin(function(jsonResponse){
                        var $dialog = null;
                        if(!jsonResponse.data){
                            $dialog = $("#login-popup");
                            $dialog.removeClass('hidden');
                            APPINFO.funs.initDialog($dialog, '登陆');
                            
                            return;
                        }
                        $.post(SYSTEM_DATA.contextPath + '/User/ajaxCommentText.shtml', {
                            'app_id' : $("#review-popup-content").attr('aId'),
                            'user_id' : $("#review-popup-content").attr('uId')
                        }, function(jsonResult){
                            if(jsonResult){
                                $("#review-popup-content").val(jsonResult.data.comment);
                                //显示写评论Dialog
                                $dialog = $("#review-popup");
                                $dialog.removeClass('hidden');
                                APPINFO.funs.initDialog($dialog, '写评论', 500, 400);
                            }
                        }, 'json');
                    });
                });
                
                //comment submit
                $("#normal-review-button").click(function(){
                    var $comment = $("#review-popup-content");
                    var commentValue = $.trim($comment.val());
                    if(4 > commentValue.length || 140 < commentValue.length){
                        alert('评论长度不符合规范');
                        return;
                    }
                    $.post(SYSTEM_DATA.contextPath + '/User/ajaxComment.shtml', {
                        'app_id' : $comment.attr('aId'),
                        'user_id' : $comment.attr('uId'),
                        'comment' : commentValue
                    }, function(jsonResult){
                        if(jsonResult.data){
                            window.location.reload();
                        }
                    }, 'json');
                });
            },
            bindLogin : function(){
                $("#a_app_info_login").click(function(){
                    //var $form = $("#login-form");
                    
                    
                    //$form.submit();
                    
                    $.post(SYSTEM_DATA.contextPath + '/User/ajaxLogin.shtml', {
                        'email' : $("#id_username").val(),
                        'password' : $("#id_password").val()
                    }, function(jsonResponse){
                        if(jsonResponse.data){
                            window.location.reload();
                        }else{
                            //显示错误信息
                            alert('用户名或密码不正确');
                        }
                    }, 'json');
                });
            }
        },
        funs : {
            initDialog : function($dialog, title){
                $dialog.dialog({
                    'title' : title,
                    'bgiframe': true,
                    'draggable' : false,
                    'resizable': false,
                    'width' : arguments[2] ? arguments[2] : 700,
                    'height': arguments[3] ? arguments[3] : 440,
                    'modal': true
                });
            },
            ajaxIsLogin : function(fun){
                $.post(SYSTEM_DATA.contextPath + '/User/isLogin.shtml', {}, fun, 'json');
            },
            //初始化评分
            initScore : function(){
                $('#span_score').jRating({
                    'bigStarsPath' : SYSTEM_DATA.p + '/js/plugin/jrating_v2.1/icons/stars.png',
                    'smallStarsPath' : SYSTEM_DATA.p + '/js/plugin/jrating_v2.1/icons/small.png',
                    'phpPath' : SYSTEM_DATA.contextPath 
                    + '/User/ajaxScore.shtml?app_id=' 
                    + $("#review-popup-content").attr('aId')
                    + '&user_id='
                    + $("#review-popup-content").attr('uId'),
                    'rateMax' : 10,
                    'isDisabled' : isCommentScore
                });
            }
        }
    };
        
    
       
}


